package com.controller.ht;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.UuidUtil;
import com.controller.base.BaseController;
import com.entity.Product;
import com.service.ProductService;

@Controller
@RequestMapping("/product/")
public class ProductController extends BaseController<Object>{

	@Autowired
	private ProductService proservice;

	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public ModelAndView findAll(Product pro,String id){
		ModelAndView mv =  new ModelAndView();
		pro.setId(id);
		if(!pro.getId().equals("0")){
			List<Product> uiList = proservice.find(pro);
			if(uiList.size()>0){
				pro = uiList.get(0);
			}
		}
		mv.addObject("pro", pro);
		mv.setViewName("pages/userInfo");
		return mv;
	}
	/**
	 * 分页查询
	 * @return
	 */
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(Product pro,Page<Product> p){
		AjaxRes res = new AjaxRes();
		Page<Product> pageUis = proservice.findByPage(pro, p);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < pageUis.getResults().size(); i++) {
			//转换时间格式
			pageUis.getResults().get(i).setTimePString(sf.format(pageUis.getResults().get(i).getTimeP()));
			pageUis.getResults().get(i).setReleaseTimeString(sf.format(pageUis.getResults().get(i).getReleaseTime()));
		}
		res.setSucceed(pageUis, "数据获取成功");
//		List<userIndustry> uis = pageUis.getResults();
//		mv.setViewName("pulicpage/index");
		return res;
	}
	/**
	 * 修改用户信息并返回修改结果
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public AjaxRes update(Product pro,String time,String publisherSession){
		SimpleDateFormat format =   new SimpleDateFormat( "yyyy-MM-dd" );
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AjaxRes res=new AjaxRes(); 
		if(pro.getId().equals("0")){
			pro.setState("暂未审核");
			pro.setId(UuidUtil.get32UUID());
			pro.setImage("");
			pro.setTimeP(date);
			pro.setAuditorTime(new Date());
			pro.setReason("无驳回原因");
			pro.setReleaseTime(new Date());
			pro.setPublisher(publisherSession);
			Integer a =	proservice.insert(pro);
			if(a==0){
				res.setResMsg("添加失败");
			}else{
				res.setResMsg("添加成功");
			}
		}else{
			pro.setTimeP(date);
			//用于测试是否修改成功的变量
			Integer a =proservice.update(pro);
			if(a==0){
				res.setResMsg("修改失败");
			}else{
				res.setResMsg("修改成功");
			}
		}
		return res;
	}
	@RequestMapping("/delect")
	@ResponseBody
	public AjaxRes delect(Product pro){
		AjaxRes res = new AjaxRes();
		//用于测试是否添加成功的变量
		Integer delRes = proservice.delete(pro);
		if(delRes==0){
			res.setResMsg("删除失败");
		}else{
			res.setResMsg("删除成功");
		}
		return res;
	}

	//批量删除
	@RequestMapping("/deleteBatch")
	@ResponseBody
	public AjaxRes deleteBatch(String uiCheck){
		AjaxRes res=new AjaxRes(); 
		List uiList=new ArrayList();
		String[] getArr = uiCheck.split(",");
		for(int i=0;i<getArr.length;i++){
			if(i!=0){
				Product pro = new Product();
				pro.setId(getArr[i]);
				uiList.add(pro);
			}
		}
		Integer a = proservice.deleteBatch(uiList);
		if(a==0){
			res.setResMsg("批量删除失败");
		}else{
			res.setResMsg("批量删除成功");
		}
		return res;
	}
	@RequestMapping("/productInfo")
	public String productInfo(Model model,String id,String timePString){
		Product pro = new Product();
		pro.setId(id);
		System.out.println(id+"===================================");
		if(!id.equals("0")){
			List<Product> proList = proservice.find(pro);
			if(proList.size()>0){
				pro = proList.get(0);
			}
		}
		pro.setTimePString(timePString);
		System.out.println(pro.toString());
		model.addAttribute("pro", pro);
		return "pages/ProductInfo";
	}
	@RequestMapping("/productState")
	public String productState(Model model,Product pro){
		String state = pro.getState();
		List<Product> proList = proservice.find(pro);
		if(proList.size()>0){
			pro = proList.get(0);
		}
		if(state.equals("100")){
			pro.setState("审核成功");
		}
		if(state.equals("200")){
			pro.setState("审核失败");
		}
		int a = proservice.update(pro);
		model.addAttribute("pro", pro);
		return "pages/ProductContent";
	}
}
